package it.stasbranger.myproject.client.managed.ui;

import com.google.gwt.core.client.GWT;

import com.google.gwt.dom.client.DivElement;
import com.google.gwt.dom.client.Element;

import com.google.gwt.dom.client.Style.Display;

import com.google.gwt.editor.client.EditorError;

import com.google.gwt.event.dom.client.ClickEvent;

import com.google.gwt.requestfactory.client.RequestFactoryEditorDriver;

import com.google.gwt.safehtml.shared.SafeHtmlBuilder;

import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;

import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.IntegerBox;
import com.google.gwt.user.client.ui.TextBox;

import it.stasbranger.myproject.client.managed.activity.SuppliersEditActivityWrapper;

import it.stasbranger.myproject.client.managed.request.SuppliersHasRequestsProxy;
import it.stasbranger.myproject.client.managed.request.SuppliersProxy;

import java.util.Collection;
import java.util.List;

public class SuppliersEditView extends Composite implements
		SuppliersEditActivityWrapper.View<SuppliersEditView> {

	interface Binder extends UiBinder<HTMLPanel, SuppliersEditView> {
	}

	interface Driver extends
			RequestFactoryEditorDriver<SuppliersProxy, SuppliersEditView> {
	}

	private static final Binder BINDER = GWT.create(Binder.class);

	private static SuppliersEditView instance;
	@UiField
	IntegerBox idsuppliers;
	@UiField
	TextBox name;
	@UiField
	TextBox address;

	@UiField
	SuppliersHasRequestsSetEditor suppliersHasRequestses;

	@UiField
	Button cancel;
	@UiField
	Button save;
	@UiField
	DivElement errors;

	@UiField
	Element editTitle;
	@UiField
	Element createTitle;

	private Delegate delegate;

	public static SuppliersEditView instance() {
		if (instance == null) {
			instance = new SuppliersEditView();
		}

		return instance;
	}

	public SuppliersEditView() {
		initWidget(BINDER.createAndBindUi(this));
	}

	public RequestFactoryEditorDriver<SuppliersProxy, SuppliersEditView> createEditorDriver() {
		RequestFactoryEditorDriver<SuppliersProxy, SuppliersEditView> driver = GWT.create(Driver.class);
		driver.initialize(this);
		return driver;
	}

	public void setSuppliersHasRequestsesPickerValues(Collection<SuppliersHasRequestsProxy> values) {
		suppliersHasRequestses.setAcceptableValues(values);

	}

	public void setCreating(boolean creating) {
		if (creating) {
			editTitle.getStyle().setDisplay(Display.NONE);
			createTitle.getStyle().clearDisplay();
		} else {
			editTitle.getStyle().clearDisplay();
			createTitle.getStyle().setDisplay(Display.NONE);
		}
	}

	public void setDelegate(Delegate delegate) {
		this.delegate = delegate;
	}

	public void setEnabled(boolean enabled) {
		save.setEnabled(enabled);
	}

	public void showErrors(List<EditorError> errors) {
		SafeHtmlBuilder b = new SafeHtmlBuilder();
		for (EditorError error : errors) {
			b.appendEscaped(error.getMessage()).appendHtmlConstant("<br>");
		}
		this.errors.setInnerHTML(b.toSafeHtml().asString());
	}

	@UiHandler("cancel")
	void onCancel(ClickEvent event) {
		delegate.cancelClicked();
	}

	@UiHandler("save")
	void onSave(ClickEvent event) {
		delegate.saveClicked();
	}
}
